<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="边坡名称" prop="slopeName">
        <el-input
          v-model="queryParams.slopeName"
          placeholder="请输入边坡名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="边坡编号" prop="slopeCode">
        <el-input
          v-model="queryParams.slopeCode"
          placeholder="请输入边坡编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="技术状况评定等级" prop="techConditionLevel" label-width="130px">
        <el-select v-model="queryParams.techConditionLevel" placeholder="请选择等级" clearable style="width: 200px">
          <el-option
            v-for="dict in tech_condition_level"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['slope:info:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['slope:info:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['slope:info:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['slope:info:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table scrollbar-always-on v-loading="loading" :data="infoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="边坡名称" align="center" fixed="left" prop="slopeName" width="220"/>
      <el-table-column label="边坡编号" align="center" fixed="left" prop="slopeCode" width="220"/>
      <el-table-column label="边坡起点桩号" align="center" prop="startPileNo" width="150"/>
      <el-table-column label="边坡终点桩号" align="center" prop="endPileNo" width="180"/>
      <el-table-column label="边坡高度" align="center" prop="slopeHeight" />
      <el-table-column label="边坡长度" align="center" prop="slopeLength" />
      <el-table-column label="路线编号" align="center" prop="routeCode" />
      <el-table-column label="路线名称" align="center" prop="routeName" />
      <el-table-column label="坡体特征" align="center" prop="slopeFeatures" />
      <el-table-column label="坡后情况" align="center" prop="slopeBack" />
      <el-table-column label="管养单位" align="center" prop="deptId" width="180">
      <template #default="scope">
        <dict-tag :options="sys_dept_list" :value="scope.row.deptId"/>
      </template>
      </el-table-column>
      <el-table-column label="技术状况评定等级" align="center" prop="techConditionLevel">
        <template #default="scope">
          <dict-tag :options="tech_condition_level" :value="scope.row.techConditionLevel"/>
        </template>
      </el-table-column>
      <el-table-column label="评定日期" align="center" prop="updateTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="政区代码" align="center" prop="regionCode" />
      <el-table-column label="边坡与路线关系" align="center" prop="slopePosition" width="150">
        <template #default="scope">
          <dict-tag :options="slope_position" :value="scope.row.slopePosition"/>
        </template>
      </el-table-column>
      <el-table-column label="起点经度" align="center" prop="startLongitude" />
      <el-table-column label="起点纬度" align="center" prop="startLatitude" />
      <el-table-column label="止点经度" align="center" prop="endLongitude" />
      <el-table-column label="止点纬度" align="center" prop="endLatitude" />
      <el-table-column label="边坡全貌照" align="center" prop="img" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.img" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['slope:info:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['slope:info:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改边坡信息对话框 -->
    <el-dialog :title="title" v-model="open" width="1000px" append-to-body>
      <el-form ref="infoRef" :model="form" :rules="rules" label-width="150px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="边坡名称" prop="slopeName">
              <el-input v-model="form.slopeName" placeholder="请输入边坡名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="边坡编号" prop="slopeCode">
              <el-input v-model="form.slopeCode" placeholder="请输入边坡编号" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="边坡起点桩号" prop="startPileNo">
              <el-input v-model="form.startPileNo" placeholder="请输入边坡起点桩号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="边坡终点桩号" prop="endPileNo">
              <el-input v-model="form.endPileNo" placeholder="请输入边坡终点桩号" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="边坡高度" prop="slopeHeight">
              <el-input v-model="form.slopeHeight" placeholder="请输入边坡高度" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="边坡长度" prop="slopeLength">
              <el-input v-model="form.slopeLength" placeholder="请输入边坡长度" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="路线编号" prop="routeCode">
              <el-input v-model="form.routeCode" placeholder="请输入路线编号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="路线名称" prop="routeName">
              <el-input v-model="form.routeName" placeholder="请输入路线名称" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="管养单位" prop="deptId">
              <el-select v-model="form.deptId" placeholder="请选择管养单位">
                <el-option
                    v-for="dict in sys_dept_list"
                    :key="dict.value"
                    :label="dict.label"
                    :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="技术状况评定等级" prop="techConditionLevel">
              <el-select v-model="form.techConditionLevel" placeholder="请选择技术状况评定等级">
                <el-option
                    v-for="dict in tech_condition_level"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="政区代码" prop="regionCode">
              <el-input v-model="form.regionCode" placeholder="请输入政区代码" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="边坡与路线关系" prop="slopePosition">
              <el-select v-model="form.slopePosition" placeholder="请选择边坡与路线关系">
                <el-option
                    v-for="dict in slope_position"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="起点经度" prop="startLongitude">
              <el-input v-model="form.startLongitude" placeholder="请输入起点经度" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="起点纬度" prop="startLatitude">
              <el-input v-model="form.startLatitude" placeholder="请输入起点纬度" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="止点经度" prop="endLongitude">
              <el-input v-model="form.endLongitude" placeholder="请输入止点经度" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="止点纬度" prop="endLatitude">
              <el-input v-model="form.endLatitude" placeholder="请输入止点纬度" />
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item label="边坡全貌照" prop="img">
              <image-upload v-model="form.img" />
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item label="坡体特征" prop="slopeFeatures">
              <el-input v-model="form.slopeFeatures" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item label="坡后情况" prop="slopeBack">
              <el-input v-model="form.slopeBack" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

  </div>
</template>

<script setup name="Info">
import { listInfo, getInfo, delInfo, addInfo, updateInfo } from "@/api/slope/info/info.js";

const { proxy } = getCurrentInstance();
const { tech_condition_level, slope_position, sys_dept_list } = proxy.useDict('tech_condition_level', 'slope_position', 'sys_dept_list');

const infoList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    slopeName: null,
    slopeCode: null,
    techConditionLevel: null,
  },
  rules: {
    slopeName: [
      { required: true, message: "边坡名称不能为空", trigger: "blur" }
    ],
    slopeCode: [
      { required: true, message: "边坡编号不能为空", trigger: "blur" }
    ],
    startPileNo: [
      { required: true, message: "边坡起点桩号不能为空", trigger: "blur" }
    ],
    endPileNo: [
      { required: true, message: "边坡终点桩号不能为空", trigger: "blur" }
    ],
    slopeHeight: [
      { required: true, message: "边坡高度不能为空", trigger: "blur" }
    ],
    slopeLength: [
      { required: true, message: "边坡长度不能为空", trigger: "blur" }
    ],
    routeCode: [
      { required: true, message: "路线编号不能为空", trigger: "blur" }
    ],
    routeName: [
      { required: true, message: "路线名称不能为空", trigger: "blur" }
    ],
    regionCode: [
      { required: true, message: "政区代码不能为空", trigger: "blur" }
    ],
    slopePosition: [
      { required: true, message: "边坡与路线关系不能为空", trigger: "change" }
    ],
    startLongitude: [
      { required: true, message: "起点经度不能为空", trigger: "blur" }
    ],
    startLatitude: [
      { required: true, message: "起点纬度不能为空", trigger: "blur" }
    ],
    endLongitude: [
      { required: true, message: "止点经度不能为空", trigger: "blur" }
    ],
    endLatitude: [
      { required: true, message: "止点纬度不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);



/** 查询边坡信息列表 */
function getList() {
  loading.value = true;
  listInfo(queryParams.value).then(response => {
    infoList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    slopeName: null,
    slopeCode: null,
    startPileNo: null,
    endPileNo: null,
    slopeHeight: null,
    slopeLength: null,
    routeCode: null,
    routeName: null,
    slopeFeatures: null,
    deptName: null,
    slopeBack: null,
    techConditionLevel: null,
    updateTime: null,
    regionCode: null,
    slopePosition: null,
    startLongitude: null,
    startLatitude: null,
    endLongitude: null,
    endLatitude: null,
    img: null,
    remark: null
  };
  proxy.resetForm("infoRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加边坡信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getInfo(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改边坡信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["infoRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateInfo(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addInfo(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除边坡信息编号为"' + _ids + '"的数据项？').then(function() {
    return delInfo(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('slope/info/export', {
    ...queryParams.value
  }, `info_${new Date().getTime()}.xlsx`)
}

getList();
</script>
